<template>
  <div>
    <el-row style="margin-top: 20px;">
      <el-col :span="12" :offset="4">
        <el-form :model="formData" :rules="rules" ref="formData" label-width="110px" class="demo-formData">
          <el-form-item label="活动名称" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item label="活动地点" prop="address">
            <el-select v-model="formData.address" placeholder="请选择">
              <el-option
                v-for="item in options1"
                :key="item.value"
                :label="item.label"
                :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="活动对象" prop="object">
            <el-select v-model="formData.object" placeholder="请选择">
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <!--          后期加回来-->
          <!--          <el-form-item label="联系电话" prop="phone">-->
          <!--            <el-input v-model.number="formData.phone" maxLength="11"></el-input>-->
          <!--          </el-form-item>-->
          <el-form-item label="微信" prop="wechat">
            <el-input v-model.number="formData.wechat" maxLength="20"></el-input>
          </el-form-item>
          <el-form-item label="参与人数" prop="maxPerson">
            <el-input-number v-model="formData.maxPerson" :min="1" :max="1000"></el-input-number>
          </el-form-item>
          <el-form-item label="活动分类" prop="activityType">
            <el-select v-model="formData.activityType" placeholder="请选择">
              <el-option
                v-for="item in options3"
                :key="item.value"
                :label="item.label"
                :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="活动开始时间" prop="beginTime">
            <el-date-picker
              v-model="formData.beginTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm"
              placeholder="选择日期时间"
              :picker-options="pickerOptionsStart">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="活动结束时间" prop="endTime">
            <el-date-picker
              v-model="formData.endTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm"
              placeholder="选择日期时间"
              :disabled="formData.beginTime==null"
              :picker-options="pickerOptionsEnd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="活动简介" prop="activityIntroduce">
            <el-input
              type="textarea"
              placeholder="请输入内容(100字内)"
              :maxlength="100"
              rows="2"
              v-model="formData.activityIntroduce"
              show-word-limit>
            </el-input>
          </el-form-item>
          <el-form-item label="活动详情" prop="activityDetail">
            <el-input
              type="textarea"
              placeholder="请输入内容(200字内)"
              :maxlength="200"
              rows="3"
              v-model="formData.activityDetail"
              show-word-limit>
            </el-input>
          </el-form-item>
          <!--          图片模块，后期可加-->
          <!--          <el-form-item label="添加图片" :prop="this.formData.photo">-->
          <!--            <el-upload-->
          <!--              class="avatar-uploader"-->
          <!--              action="http://localhost:8080/api/covers"-->
          <!--              :show-file-list="false"-->
          <!--              :on-success="handleAvatarSuccess"-->
          <!--              :before-upload="beforeAvatarUpload">-->
          <!--              <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">-->
          <!--              <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
          <!--            </el-upload>-->
          <!--          </el-form-item>-->
          <el-form-item class="button_submit">
            <el-button type="primary" @click="submitForm('formData')">立即创建</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {addActivity} from "../../../api/activity";

  export default {
    name: "addActivity",
    data() {
      return {
        imageUrl: '',
        formData: {
          clubId: this.$route.params.clubId,
          name: '', // 名称
          clubName: '',
          address: '', // 地址
          activityIntroduce: '', // 介绍
          wechat: '',
          object: '',
          // kind: '',
          activityType: '',
          activityDetail: '',
          maxPerson: '',
          activityStatus: 0,
          // photo: '',
          beginTime: '',
          endTime: ''
        },
        clubList: [],
        options1: [
          {
            value: '选项1',
            label: '足球场'
          }, {
            value: '选项2',
            label: '喷泉广场'
          }, {
            value: '选项3',
            label: '学术报告厅'
          }, {
            value: '选项4',
            label: 'E楼'
          }, {
            value: '选项5',
            label: 'A楼'
          }, {
            value: '选项6',
            label: 'B楼'
          }, {
            value: '选项7',
            label: 'C楼'
          }, {
            value: '选项8',
            label: '体育馆'
          }
        ],
        options2: [
          {
            value: '选项1',
            label: '软件工程系'
          }, {
            value: '选项2',
            label: '数码媒体系'
          }, {
            value: '选项3',
            label: '计算机系'
          }, {
            value: '选项4',
            label: '网络技术系'
          }, {
            value: '选项5',
            label: '外语系'
          }, {
            value: '选项6',
            label: '财会系'
          }, {
            value: '选项7',
            label: '管理系'
          }, {
            value: '选项8',
            label: '全体学生'
          }
        ],
        options3: [
          {
            value: '1',
            label: '文娱'
          }, {
            value: '2',
            label: '体育'
          }, {
            value: '3',
            label: '科技'
          }, {
            value: '4',
            label: '素拓'
          }, {
            value: '5',
            label: '讲座'
          }, {
            value: '6',
            label: '学术'
          }
        ],
        rules: {
          name: [
            {required: true, message: '请输入活动名称', trigger: 'blur'}
          ],
          address: [
            {required: true, message: '请输入详细地址', trigger: 'blur'}
          ],
          wechat: [
            {required: true, message: '请输入详细微信', trigger: 'blur'}
          ],
          // phone: [
          //   {required: true, message: '请输入联系电话'},
          //   {type: 'number', message: '电话号码必须是数字'}
          // ],
          object: [
            {required: true, message: '请选择对象', trigger: 'blur'}
          ],
          activityType: [
            {required: true, message: '请选择类型', trigger: 'blur'}
          ],
          beginTime: [
            {required: true, message: '请选择时间', trigger: 'blur'}
          ],
          endTime: [
            {required: true, message: '请选择时间', trigger: 'blur'}
          ]
        },
        pickerOptionsStart: {
          disabledDate: time => {
            let endDateVal = this.formData.endTime;
            if (endDateVal) {
              return time.getTime() > new Date(endDateVal).getTime();
            } else {
              return time.getTime() < Date.now() - 8.64e7;
            }
          }
        },
        pickerOptionsEnd: {
          disabledDate: time => {
            let beginDateVal = this.formData.beginTime;
            if (beginDateVal) {
              return (time.getTime() < new Date(beginDateVal).getTime()
              )
            } else {
              return time.getTime() < Date.now() - 8.64e7;
            }
          },
        }
      }
    },
    watch: {
      'formData.beginTime'() {
        if (this.formData.beginTime == null) {
          this.formData.endTime = null
        }
      }
    },
    created() {
    },
    methods: {

      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
            if (valid) {
              console.log("时间格式：",this.formData.endTime)
              Object.assign(this.formData)
              addActivity(this.formData).then(res => {
                console.log(res)
                if (res.code == 200) {
                  console.log(111)
                  this.$message({
                    type: 'success',
                    message: res.message
                  });
                  // this.$router.push('/managerHome/activityManage')
                  this.$router.back();
                } else {
                  this.$message({
                    type: 'error',
                    message: res.message
                  });
                }
              })
            } else {
              this.$notify.error({
                title: '错误',
                message: '请检查输入是否正确',
                offset: 100
              });
              return false;
            }
          }
        )
        ;
      }
    }
  }
</script>

<style scoped>

</style>
